﻿
using System;
using System.Data;
using System.Data.OleDb;
using Slc.Core.Enums;

namespace Slc.Core.DataProvider
{
   /// <summary>
   /// Access 数据库访问类
   /// </summary>
    public sealed class Access : OleDb 
    {

        /// <summary>
        /// 获取当前数据库类型的枚举
        /// </summary>
        public override DataDbType CurrentDbType
        {
            get { return DataDbType.ACCESS; }
        }

        private string _insertKey;
        /// <summary>
        /// 在插入具有自增列的数据后，获取刚才自增列的数据的
        /// </summary>
        public override string InsertKey
        {
            get
            {
                if (string.IsNullOrEmpty(_insertKey))
                    return "SELECT @@IDENTITY";
                else
                    return _insertKey;
            }
            set
            {
                _insertKey = value;
            }
        }
        public override IDataParameter GetParameter(string paraName, object Value)
        {
            IDataParameter para = base.GetParameter(paraName, Value);
            if (Value is DateTime)
                ((OleDbParameter)para).OleDbType = OleDbType.DBDate;
            return para;
        }

        public override IDataParameter GetParameter(string paraName, DbType dbType)
        {
            IDataParameter para = this.GetParameter();
            para.ParameterName = paraName;
            para.DbType = dbType;
            if (dbType == DbType.DateTime)
                ((OleDbParameter)para).OleDbType = OleDbType.DBDate;
            return para;
        }

        public override IDataParameter GetParameter(string paraName, System.Data.DbType dbType, int size)
        {
            OleDbParameter para = new OleDbParameter();
            para.ParameterName = paraName;
            para.DbType = dbType;
            para.Size = size;
            if (dbType == DbType.DateTime)
                ((OleDbParameter)para).OleDbType = OleDbType.DBDate;
            return para;
        }

        public override string GetNativeDbTypeName(IDataParameter para)
        {
            OleDbType type = ((OleDbParameter)para).OleDbType;
            if (type == OleDbType.VarWChar)
                type = OleDbType.VarChar;
            else if (type == OleDbType.DBDate)
                type = OleDbType.Date;
            return type.ToString();
        }
    }
    
}
